<template>
  <div>
    <div class="login" id="app">
    <div class="sign">綁定平臺賬戶</div>
    <img class="weyouth" src="../style/img/weyouth.png" />
    <div>
        <input v-model='phone' placeholder="請輸入手機號">
    </div>
    <div class="gray"></div>
    <div>
        <input v-model='password' placeholder="請輸入密碼" type="password">
    </div>
    <div class="gray"></div>
    <!--<div class="input">
        <input type="checkbox" id="checkbox">
        <span>同意協議</span>
    </div>-->
    <button @click="bind()">綁定</button>
    <div>
        <div class="forget">忘記密碼</div>
        <div class="Vertical"></div>
        <div class="regis" @click="reg()">立即注冊 </div>
    </div>
</div>
  </div>
</template>

<script>
import { getUnionId, wxBind } from '@/api/login'
export default {
  name: 'WxBind',
  data () {
    return {
      phone: '',
      password: '',
      unionid: ''
    }
  },
  created () {
    this.getUnionId()
  },
  methods: {
    getUnionId () {
      getUnionId().then(res => {
        if (res.code === 1) {
          this.unionid = res.data.unionid
        } else {
          this.$router.push('/login')
        }
      })
    },
    reg () {
      this.$router.push('/register?unionid=' + this.unionid)
    },
    bind () {
      var phone = this.phone
      var pwd = this.pwd
      if (phone !== '' && pwd !== '') {
        wxBind({
          phone: phone,
          password: pwd,
          unionid: this.unionid
        }).then(res => {
          if (res.code === 1) {
            localStorage.setItem('isLogin', 'isLogin')
            this.$router.push('/')
          } else {
            alert('綁定失敗，請重新綁定！' + res.msg)
          }
        }).catch(err => {
          console.log(err)
          alert('请检查账号密码后重新登录')
        })
      }
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

body {
  background: rgb(255, 255, 255);
  font-family: PingFangSC;
}
.am-offcanvas {
  position: relative
}
.login {
  width: 80%;
  margin: 30px auto auto auto;
}

.sign {
  width: 100%;
  text-align: center;
  font-size: 30px;
  margin-bottom: 30px;
}

input {
  font-size: 17px;
}

input::-webkit-input-placeholder {
  font-size: 17px;
  line-height: 44px;
}

.login input {
  line-height: 30px;
  border: none;
  outline: none;
}

.gray {
  margin: 5px 0 20px 0;
  height: 1px;
  background: rgb(212, 212, 212);
}

.input {
  color: gray;
}

button {
  margin: 40px auto 20px auto;
  width: 100%;
  height: 50px;
  border-radius: 30px;
  background: rgb(249, 119, 130);
  border: none;
  font-size: 24px;
  color: white;
  outline: none;
}

.forget {
  float: left;
  text-align: right;
  width: 40%;
  margin: 0 28px 0 0;
}

.Vertical {
  margin: 3px 0 0 0;
  float: left;
  background: rgb(180, 187, 201);
  height: 20px;
  width: 1px;
}

.regis {
  float: right;
  text-align: left;
  width: 40%;
}

.other {
  overflow: hidden;
  width: 100px;
  height: 100px;
  margin: 100px auto auto auto;
}

.pic {
  margin: 0 30px;
  background: url(../style/img/wxchat.png) no-repeat;
  height: 40px;
  width: 40px;
  background-size: cover;
}

.mode {
  width: 100%;
  text-align: center;
  color: gray;
  margin-top: 1px;
}

.input input {
  float: left;
}

.input span {
  display: inline-block;
  margin-top: -4px;
  margin-left: 4px;
  float: left;
}

.weyouth {
  width: 100px;
  margin-bottom: 10px;
}
</style>
